package com.example.administrator.hqq.activity.module;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ApplyForNewModuleActivity extends MyActivity {

    @BindView(R.id.return_auto)
    AutoRelativeLayout returnAuto;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.report_auto)
    AutoRelativeLayout reportAuto;
    @BindView(R.id.module_name)
    EditText moduleName;
    @BindView(R.id.reason)
    EditText reason;
    @BindView(R.id.submit)
    Button submit;
    private String TAG=this.getClass().getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_for_new_module);
        ButterKnife.bind(this);
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }

    @OnClick({R.id.return_auto, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_auto:
                finish();
                break;
            case R.id.submit:
                applyForNewModule();
                break;
        }
    }

    private void applyForNewModule() {
        if (!TextUtils.isEmpty(moduleName.getText())&&!TextUtils.isEmpty(reason.getText()))
        {
            OkHttpUtils.get().url(UrlFactory.ApplyNewTopic())
                    .addParams(LoongsConfig.user_id,LoongsConfig.getUser_id())
                    .addParams(LoongsConfig.name,moduleName.getText().toString())
                    .addParams(LoongsConfig.reason,reason.getText().toString())
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Log.e(TAG, "onError: "+e );
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.e(TAG, "onResponse: "+response );
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (jsonObject.getInt(LoongsConfig.code)==200)
                        {
                            Intent intent = new Intent(ApplyForNewModuleActivity.this, AddModuleFeedbackActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }else{
            ToastUtils.showShort("请填写完申请资料");
        }
    }
}
